<?php
$loader->requireOnce('ordo/Insurance.class.php');
$loader->requireOnce('ordo/TicketItem.class.php');
$loader->requireOnce('includes/ServiceDiscount/Discountable.class.php');
$loader->requireOnce('includes/ServiceDiscount/ServiceDiscountException.class.php');

abstract class TicketItemPayment {
	
	/**
	 * The final result
	 *
	 * @var array
	 *   [original_price
	 *    patient_price,
	 *    insurance_price,
	 *    discount]
	 */
	protected $result = array();		
	
	/**
	 * Implement Discountable interface
	 *
	 * @var DiscountExecuter
	 */
	protected $discountBehaviour;
		
	function __construct($discountBehaviour) {
		if ($discountBehaviour instanceof Discountable ) {
			$this->discountBehaviour = $discountBehaviour;
		} else {
			throw new ServiceDiscountException('The discount behaviour must implement Discountable interface');
		}
	}
	
	
	/**
	 * Calculate prices for adding new ticket item
	 * Service prices are retrieved from service table
	 *
	 * @param int $ticketItemId
	 * @param String $insuranceType
	 */
	public abstract function add($ticketItemId, $insuranceType);
	
	/**
	 * Calculate prices for updating ticket item
	 * Service prices are retrieved from ticket item table
	 *
	 * @param int $ticketItemId
	 * @param String $insuranceType
	 */
	public abstract function update($ticketItemId);
	
	
	public function setDiscountExecuter($executer) {
		if ($executer instanceof Discountable ) {
			$this->$discountBehaviour = $executer;
		}		
	}
	
	/**
	 * Getter to get the result price
	 *
	 * @return array
	 */
	public function getResult() {
		return $this->result;
	}
		
	
	/**
	 * Get price of a service based on insurance type
	 *
	 * @param int $serviceId
	 * @param String $insuranceType
	 */
	public function getServicePrice($serviceId, $insuranceType) {
		$service = Celini::newORDO('Service', $serviceId);					
		
		$originalPrice = $service->get('price');
		$patientPrice = $originalPrice;
		$insurancePrice = 0;
				
		if ($insuranceType == Insurance::REQUIRED) {
			$insurancePrice = $service->get('required_insurance_price');
			$patientPrice = $service->get('patient_required_insurance_price');
		} else if ($insuranceType == Insurance::VOLUNTEERED) {
			$insurancePrice = $service->get('volunteered_insurance_price');
			$patientPrice = $service->get('patient_volunteered_insurance_price');
		}
		
		return array('original_price' => intval($originalPrice),
				'insurance_price'	=> intval($insurancePrice),
				'patient_price'		=> intval($patientPrice));
	}	
	
	public function getTicketItemPrice($ticketItemId) {
		$ti = Celini::newORDO('TicketItem', $ticketItemId);
		return array(
					'original_price'	=>intval($ti->get('original_price')),
					'insurance_price'	=>intval($ti->get('insurance_price')),
					'patient_price'	=>intval($ti->get('patient_price')));
	}
}
?>